﻿module CPFD.Utils

    module Array =
        let rand = new System.Random()
 
        let swap (a: _[]) x y =
            let tmp = a.[x]
            a.[x] <- a.[y]
            a.[y] <- tmp
 
        // ref: http://tryfs.net/snippets/snippet-L
        // shuffle an array (in-place)
        let shuffle a =
            Array.iteri (fun i _ -> swap a i (rand.Next(i, Array.length a))) a

    module List =
        let private rand = new System.Random()
        let randomize xs = List.sortBy (fun _ -> rand.Next()) xs
        let selectRandom count xs = xs |> randomize |> Seq.take count |> Seq.toList
        let pickRandom xs = 
            let i = rand.Next(0, List.length xs - 1)
            List.nth xs i
        let rec set i newval xs =
            match i, xs with
            | 0, x::xs' -> newval::xs'
            | i, x::xs' -> x :: set (i-1) newval xs'
            | _ -> failwith "invalid argument"
